package com.cskaoyan.service.impl;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.admin.vo.AuthoritySonVO;
import com.cskaoyan.bean.admin.vo.AuthorityVO;
import com.cskaoyan.mapper.AuthorityMapper;
import com.cskaoyan.mapper.PermissionMapper;
import com.cskaoyan.bean.admin.vo.RoleOptionVO;
import com.cskaoyan.mapper.RoleMapper;
import com.cskaoyan.service.RoleService;
import com.cskaoyan.utils.CommonUtil;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @PROJECT_NAME: market_34th
 * @DESCRIPTION:
 * @Author: qiu
 * @DATE: 2021/10/18 23:46
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    RoleMapper roleMapper;

    @Autowired
    AuthorityMapper authorityMapper;

    @Autowired
    PermissionMapper permissionMapper;

    /**
     * 角色分页信息
     * @param param
     * @return
     */
    @Override
    public BasePageData<Role> query(String name, BaseParam param) {
        // 配置分页信息
        PageHelper.startPage(param.getPage(), param.getLimit());
        // 构造条件的对象
        RoleExample example = new RoleExample();
        RoleExample.Criteria criteria = example.createCriteria();
        if (CommonUtil.isStrNotEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        // 设置排序
        example.setOrderByClause(param.getSort() + " " + param.getOrder());
        List<Role> roles = roleMapper.selectByExample(example);

        return BasePageData.ok(roles);

    }

    /**
     * 角色信息选项
     * @return
     */
    @Override
    public BasePageData<RoleOptionVO> getOptions() {

        RoleExample example = new RoleExample();

        // 数据的查询和封装
        List<Role> roles = roleMapper.selectByExample(example);
        List<RoleOptionVO> options = new ArrayList<RoleOptionVO>();
        for (Role role : roles) {
            RoleOptionVO op = new RoleOptionVO();
            op.setValue(role.getId());
            op.setLabel(role.getName());
            options.add(op);
        }

        // 封装到分页对象
        BasePageData pageData = new BasePageData();
        pageData.setList(options);

        return pageData;
    }

    /**
     * 创建角色（权限）
     * @param name
     * @param desc
     * @return
     */
    @Override
    public Role create(String name, String desc) {

        // 校验角色名是否已经存在
        RoleExample example = new RoleExample();
        RoleExample.Criteria criteria = example.createCriteria();
        criteria.andNameEqualTo(name);
        List<Role> queryRole = roleMapper.selectByExample(example);
        if(queryRole.size() != 0){
            return null;
        }

        // 角色名不存在，新建角色
        Role role = new Role();
        role.setName(name);
        role.setDesc(desc);
        role.setAddTime(new Date());
        role.setUpdateTime(new Date());
        roleMapper.insertSelective(role);
        if(role.getDesc() == null){
            role.setDesc("");
        }
        return role;
    }

    /**
     * 删除角色
     * @param role
     */
    @Override
    public void delete(Role role) {
        roleMapper.deleteByPrimaryKey(role.getId());
    }

    /**
     * 修改角色
     * @param role
     */
    @Override
    public void update(Role role) {
        // 需要更新修改时间
        role.setUpdateTime(new Date());

        roleMapper.updateByPrimaryKeySelective(role);
    }

    /**
     * 角色所拥有的权限
     * @param roleId
     * @return
     */
    @Override
    public AuthorityVO permissions(Integer roleId) {

        // 1.获取三级联动数据
        // 先查询一级数据，即父级id为0的数据
        List<AuthoritySonVO> firsts = authorityMapper.selectAuthorityByPid(0);

        // 遍历一级数据，根据pid获取二级数据..以此类推获取三级数据
        for (AuthoritySonVO first : firsts) {
            Integer firstPID = first.getPrimaryId();
            List<AuthoritySonVO> seconds = authorityMapper.selectAuthorityByPid(firstPID);
            first.setChildren(seconds);

            for (AuthoritySonVO second : seconds) {
                Integer secondPID = second.getPrimaryId();
                List<AuthoritySonVO> thirds = authorityMapper.selectAuthorityByPid(secondPID);
                second.setChildren(thirds);
            }
        }

        // 2.获取角色的权限byRoleId
        List<String> assignedPermissions = permissionMapper.selectPermsByRoleId(roleId);

        // 3.数据封装
        AuthorityVO authorityVO = new AuthorityVO();
        authorityVO.setSystemPermissions(firsts);
        authorityVO.setAssignedPermissions(assignedPermissions);

        return authorityVO;
    }

    /**
     * 根据多个角色id返回多个管理员名字
     * @param roleIds
     * @return
     */
    @Override
    public List<String> queryNamesByIds(List<Integer> roleIds) {
        return roleMapper.queryNamesByIds(roleIds);
    }


}
